package uk.co.biggroup.lotto.syndicate;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.apache.http.client.ClientProtocolException;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.xml.sax.SAXException;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class SecondLevel extends ListActivity {

	public static String link;
	public static int drawid;
	public static String[] numbers;
	public static String[] links;
	public static String newlink;
	public static String[] filelinks;
	public static String balance;
	public static String luckynumbers;	
	public static final String PREFS_NAME = "Settings";
	
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		Bundle bundle = this.getIntent().getExtras();
		
	//	linkx = bundle.getString("link");
		drawid = bundle.getInt("drawid");
		link = Integer.toString(drawid);
	//	balance = bundle.getString("balance");
	//	luckynumbers = bundle.getString("luckynumbers");	
		balance = "";
		luckynumbers = "";
		Logh.i(link, balance, luckynumbers);
		
		new DownloadList().execute("");

	}

//	protected void onListItemClick(ListView l, View v, int position, long id) {
//		super.onListItemClick(l, v, position, id);
//		newlink = links[position];

	//	Object o = this.getListAdapter().getItem(position);
	//	String ss = Integer.toString(position);
	//	String keyword = o.toString();
	//	new GetDownloadURL().execute("");
//	}

	private class DownloadList extends AsyncTask<String, Void, String> {

		ProgressDialog dialog;

		@Override
		protected String doInBackground(String... params) {
			try {
				getHtml();
			} catch (ClientProtocolException e) {
				Logh.i("ClientProtocolException");
			} catch (IOException e) {
				Logh.i("IOException");
			} catch (XPatherException e) {
				Logh.i("XPatherException");
			} catch (ParserConfigurationException e) {
				Logh.i("ParserConfigurationException");
			} catch (SAXException e) {
				Logh.i("SAXException");
			} catch (XPathExpressionException e) {
				Logh.i("XPathExpressionException");
			}

			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			dialog.dismiss();
			SecondLevel.this.setListAdapter(new ArrayAdapter<String>(
					SecondLevel.this, R.layout.grouprowlayout, R.id.label,
					numbers));
		}

		@Override
		protected void onPreExecute() {
			dialog = ProgressDialog.show(SecondLevel.this, "",
					getString(R.string.wait), true);
		}

		@Override
		protected void onProgressUpdate(Void... values) {

		}

		public void getHtml() throws ClientProtocolException, IOException,
				XPatherException, ParserConfigurationException, SAXException,
				IOException, XPatherException, XPathExpressionException {

			String NUMBERS_XPATH = "//draws/draw[@id='" + link + "']/lines//line/@numbers";
			
			TagNode node;

			ArrayList<String> numbersList = new ArrayList<String>();

			SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
			String tokenCheck = settings.getString("token", "");
			
			String fullurl = "http://syndicates.biggroup.co.uk/MyAccount/DownloadXML.aspx?t=" + tokenCheck;
			Logh.i(fullurl);
			URL url = new URL(fullurl);
			URLConnection conn = url.openConnection();

			HtmlCleaner cleaner = new HtmlCleaner();
			CleanerProperties props = cleaner.getProperties();
			props.setAllowHtmlInsideAttributes(true);
			props.setAllowMultiWordAttributes(true);
			props.setRecognizeUnicodeChars(true);
			props.setTranslateSpecialEntities(true);
			props.setOmitComments(true);

			node = cleaner.clean(new InputStreamReader(conn.getInputStream()));
					
			Object[] numbers_nodes = node.evaluateXPath(NUMBERS_XPATH);

			if (numbers_nodes.length > 0) {
				for (Object linksNode : numbers_nodes) {
					String s = linksNode.toString();
					numbersList.add(s);
					Logh.i(s);
				}
			}

			int itemCount = numbersList.size();
			numbers = new String[itemCount];
	//		links = new String[itemCount];
			Logh.i(Integer.toString(itemCount));
			for (int i = 0; i < itemCount; i++) {
				numbers[i] = numbersList.get(i);
	//			links[i] = linksList.get(i);
			}
		}

	}

}
